package com.kinghood.productcenter.controller.base;

import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.dto.StatusReqDTO;
import com.kinghood.productcenter.common.model.dto.base.AttributeCodesReqDTO;
import com.kinghood.productcenter.common.model.dto.base.AttributeReqDTO;
import com.kinghood.productcenter.common.model.dto.base.GetAttributePageListReqDTO;
import com.kinghood.productcenter.common.model.dto.base.ListAttributeQueryDTO;
import com.kinghood.productcenter.common.model.enums.base.AttributeValueCodeEnum;
import com.kinghood.productcenter.common.model.vo.base.GetAttributePageListVO;
import com.kinghood.productcenter.service.base.IBaseAttributeService;
import com.kinghood.sharding.model.PageResponseVO;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 可选项属性 前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/baseAttribute")
public class BaseAttributeController {
    @Resource
    private IBaseAttributeService iBaseAttributeService;

    /**
     * 列表
     *
     * @author jp
     * @date 2023/12/20
     */
    @PostMapping("/listAttribute")
    public KingHoodResponseVO listAttribute(@RequestBody ListAttributeQueryDTO param) {
        return KingHoodResponseVO.success(iBaseAttributeService.listAttribute(param));
    }

    /**
     * 下拉分页
     *
     * @param param 条件
     * @return 属性
     * @author zfj
     * @date 2024/1/24
     */
    @PostMapping("/getAttributePageList")
    public KingHoodResponseVO<PageResponseVO<GetAttributePageListVO>> getAttributePageList(@RequestBody GetAttributePageListReqDTO param) {

        return KingHoodResponseVO.success(iBaseAttributeService.getAttributePageList(param));
    }

    /**
     * 新增更新
     *
     * @param param
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_ATTRIBUTE,content = "属性管理-新增、修改属性")
    @PostMapping(value = "/addOrUpdateAttribute")
    public KingHoodResponseVO addOrUpdateAttribute(@Valid @RequestBody AttributeReqDTO param){
        if (AttributeValueCodeEnum.isMulti(param.getValueCode()) && CollectionUtils.isEmpty(param.getAttributeValueList())) {
            throw new KingHoodException(-1 ,"属性值不能为空");
        }
        if (AttributeValueCodeEnum.isNeedLimitInfo(param.getValueCode()) && Objects.isNull(param.getAttributeLimit())) {
            throw new KingHoodException(-1 ,"属性值控制不能为空");
        }
        iBaseAttributeService.addorUpdateAttribute(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_ATTRIBUTE,content = "属性管理-删除属性")
    @RequestMapping(value = "/delAttribute/{id}")
    public KingHoodResponseVO delAttribute(@PathVariable("id") Long id){
        iBaseAttributeService.delAttributeById(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据ID查询详情
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @GetMapping(value = "/detailAttribute/{id}")
    public KingHoodResponseVO detailAttribute(@PathVariable("id") Long id){
        return KingHoodResponseVO.success(iBaseAttributeService.detailAttribute(id));
    }

    /**
     * 批量获取属性
     *
     * @param param 参数
     * @author jp
     * @date 2024/01/03
     */
    @PostMapping(value = "/attributeByCodes")
    public KingHoodResponseVO attributeByCodes(@Valid @RequestBody AttributeCodesReqDTO param){
        return KingHoodResponseVO.success(iBaseAttributeService.attributeByCodes(param));
    }

    /**
     * 更新状态
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_ATTRIBUTE,content = "属性管理-修改状态")
    @PostMapping(value = "/updateAttributeStatus")
    public KingHoodResponseVO updateAttributeStatus(@Valid @RequestBody StatusReqDTO param){
        iBaseAttributeService.updateAttributeStatus(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 下载属性
     *
     * @param param 参数
     * @author jp
     * @date 2024/01/04
     */
    @PostMapping("/downloadAttribute")
    public void downloadAttribute(@RequestBody ListAttributeQueryDTO param, HttpServletResponse response) {
        iBaseAttributeService.downloadAttribute(param, response);
    }

    /**
     * 删除属性值检查
     *
     * @param valueId 值id
     * @return
     * @author jp
     * @date 2024/01/11 14:16
     */
    @RequestMapping("/checkAttributeValueUsed/{valueId}")
    public KingHoodResponseVO checkAttributeValueUsed( @PathVariable("valueId") Long valueId) {
        iBaseAttributeService.checkAttributeValueUsed(valueId);
        return KingHoodResponseVO.success();
    }
}
